package chess;

import java.util.concurrent.ConcurrentLinkedQueue;

public class GameRoom {
	private String seatA;
	private String seatB;
	private String status="inactive";
	private String id;
	private String turn;
	private GameHall gameHall;
	private boolean seatAready;
	private boolean seatBready;
	
	private ConcurrentLinkedQueue<IMSG> seatAmsg = new ConcurrentLinkedQueue<IMSG>();
	private ConcurrentLinkedQueue<IMSG> seatBmsg = new ConcurrentLinkedQueue<IMSG>();
	public boolean isSeatAready() {
		return seatAready;
	}
	public void setSeatAready(boolean seatAready) {
		this.seatAready = seatAready;
	}
	public boolean isSeatBready() {
		return seatBready;
	}
	public void setSeatBready(boolean seatBready) {
		this.seatBready = seatBready;
	}
	public GameRoom(String id,GameHall gHall){
		this.id = id;
		this.gameHall = gHall;
	}
	public String getSeatA() {
		return seatA;
	}
	public void setSeatA(String seatA) {
		this.seatA = seatA;
		this.gameHall.updateRoom(new RoomListMSG(this.id,"seatA",this.seatA));
	}
	public String getSeatB() {
		return seatB;
	}
	public void setSeatB(String seatB) {
		this.seatB = seatB;
		this.gameHall.updateRoom(new RoomListMSG(this.id,"seatB",this.seatB));
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
		this.gameHall.updateRoom(new RoomListMSG(this.id,"status",this.status));
		if("active".equals(status)){
			this.addMSG("A", new ReadyStateMSG("start","A"));
			this.addMSG("B", new ReadyStateMSG("start","A"));
		}
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTurn() {
		return turn;
	}
	public void setTurn(String turn) {
		this.turn = turn;
		this.addMSG("A", new ReadyStateMSG("start",turn));
		this.addMSG("B", new ReadyStateMSG("start",turn));
	}
	
	public void addMSG(String seat,IMSG msg){
		ConcurrentLinkedQueue<IMSG> msgQueue =null;
		if(seat.equals("A"))
			msgQueue = seatAmsg;
		else if(seat.equals("B"))
			msgQueue = seatBmsg;
		msgQueue.offer(msg);
	}
	public IMSG getMSG(String seat){
		ConcurrentLinkedQueue<IMSG> msg =null;
		if(seat.equals("A"))
			msg = seatAmsg;
		else if(seat.equals("B"))
			msg = seatBmsg;
		return msg.poll();
	}
}
